package com.clz.appdesign.mvvm;

import android.app.ProgressDialog;
import android.arch.lifecycle.LifecycleOwner;
import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProvider;
import android.arch.lifecycle.ViewModelStore;
import android.arch.lifecycle.ViewModelStoreOwner;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.clz.appdesign.appdependence.base.BaseActivity;
import com.clz.appdesign.myapp.R;
import com.clz.appdesign.myapp.databinding.ActivityImageSeeBinding;

public class ImageSeeActivity extends BaseActivity implements ViewModelStoreOwner,LifecycleOwner{
    private ActivityImageSeeBinding mBinding;
    private ImageViewModel mViewModel;
    private ProgressDialog mProgressDialog;
    @Override
    public void initToolbar() {

    }

    @Override
    protected void beforeInit() {
        super.beforeInit();
        mBinding= DataBindingUtil.setContentView(this,R.layout.activity_image_see);
        mViewModel=new ViewModelProvider(
                this, new ViewModelProvider.AndroidViewModelFactory(getApplication())
        ).get(ImageViewModel.class);
        mProgressDialog=new ProgressDialog(this);
        mProgressDialog.setMessage("加载中");
        mViewModel.getImage().observe(this, new Observer<Data<ImageBean.ImagesBean>>() {
            @Override
            public void onChanged(@Nullable Data<ImageBean.ImagesBean> imagesBeanData) {
                if (imagesBeanData.getErrorMessage()!=null){
                    ToastUtils.showShort(imagesBeanData.getErrorMessage());
                    mProgressDialog.dismiss();
                    return;
                }
                mBinding.setImagebean(imagesBeanData.getData());
                setTitle(imagesBeanData.getData().getCopyright());
                mProgressDialog.dismiss();
            }
        });
        mBinding.setPresenter(new OnlickPresenter());
        mViewModel.loadImage();
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {
    }

    @Override
    public void doBusiness(Context mContext) {

    }

    @Override
    public int layoutResID() {
        return R.layout.activity_image_see;
    }

    @NonNull
    @Override
    public ViewModelStore getViewModelStore() {
        return new ViewModelStore();
    }

    public class OnlickPresenter{
        public void onClick(View view){
            switch (view.getId()){
                case R.id.btn_previous:
                    mViewModel.privousImage();
                    break;
                case R.id.btn_load:
                    mViewModel.loadImage();
                    break;
                case R.id.btn_next:
                    mViewModel.nextImage();
                    break;
                default:
                    break;
            }
        }
    }
}
